package com.itheima.dubbo.mysql.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.model.domain.UserLoginRecord;
import com.itheima.dubbo.interfaces.UserLoginRecordApi;
import com.itheima.dubbo.mysql.mapper.UserLoginRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserLoginRecordApiImpl implements UserLoginRecordApi {

    @Autowired
    private UserLoginRecordMapper userLoginRecordMapper;

    @Override
    public UserLoginRecord queryUserLoginRecordById(Long userId) {
        UserLoginRecord userLoginRecord = userLoginRecordMapper.queryUserLoginRecordById(userId);
        return userLoginRecord;
    }

    @Override
    public Integer queryCountByDateAndUserIds(List<Long> userIds, Date startOfDay, int i) {
        //获取几天后的那天的初始时间
        Date startDay = DateUtil.offsetDay(startOfDay, Convert.toInt(i));
        //这一天的结束时间
        Date endDay = DateUtil.endOfDay(startDay);


        //一个用户一天登录多次，只算一次
        List<UserLoginRecord> userLoginRecords = userLoginRecordMapper.selectList(Wrappers
                .lambdaQuery(UserLoginRecord.class)
                .in(CollUtil.isNotEmpty(userIds), UserLoginRecord::getUserId, userIds)
                .between(UserLoginRecord::getLogtime, startDay, endDay)
        );
        Map<Long, UserLoginRecord> userId = CollUtil.fieldValueMap(userLoginRecords, "userId");
        return userId.size();
    }

    //分页查询日志表
    @Override
    public List<UserLoginRecord> queryLogsListByUserId(Integer page, Integer pageSize, String sortProp, String sortOrder, Integer uid) {
        Page<UserLoginRecord> selectPage = null;
        //如果不用排序
        if (ObjectUtil.hasEmpty(sortProp)) {
            selectPage = userLoginRecordMapper.selectPage(new Page<>(page, pageSize),
                    Wrappers.lambdaQuery(UserLoginRecord.class).eq(UserLoginRecord::getUserId, Convert.toLong(uid)));
        } else {
            //按照时间进行排序
            if (sortOrder.equals("ascending")) {
                //升序
                selectPage = userLoginRecordMapper.selectPage(new Page<>(page, pageSize),
                        Wrappers.lambdaQuery(UserLoginRecord.class).eq(UserLoginRecord::getUserId, Convert.toLong(uid)).orderByDesc(UserLoginRecord::getLogtime));

            } else {
                //降序
                selectPage = userLoginRecordMapper.selectPage(new Page<>(page, pageSize),
                        Wrappers.lambdaQuery(UserLoginRecord.class).eq(UserLoginRecord::getUserId, Convert.toLong(uid)).orderByAsc(UserLoginRecord::getLogtime));
            }
        }
        return selectPage.getRecords();
    }

    @Override
    public Long countLogsSize(Integer uid) {
        Integer count = userLoginRecordMapper.selectCount(Wrappers.lambdaQuery(UserLoginRecord.class).eq(UserLoginRecord::getId, uid));
        return Convert.toLong(count);
    }

    @Override
    public void saveUserLoginRecordApi(UserLoginRecord userLoginRecord) {
        userLoginRecordMapper.insert(userLoginRecord);
    }

}
